//购物车相关逻辑
import { useStore } from 'vuex'
import { toRefs, computed } from 'vue'
import { useRoute } from 'vue-router'
export const useCommodityEffect = () => {
    const store = useStore();
    const { cartList } = toRefs(store.state);
    const productChangeClick = (shopId, productId, item, num, name) => {
        store.commit("changeItem", { shopId, productId, item, num, name });
    };
    return { cartList, productChangeClick };
};
export const useCartEffect = () => {
    const store = useStore();
    const route = useRoute();
    const id = route.params.id;
    const cartList = store.state.cartList;
    const cartEffect = computed(() => {
        const products = cartList[id]?.productList;
        const result = {
            total: 0,
            price: 0,
            getCartList: [],
            chechAllChange: true,
        };
        for (const i in products) {
            if (products[i].count > 0) {
                result.getCartList.push(products[i])
            }
        }
        if (products) {
            for (let i in products) {
                const product = products[i];
                result.total += product.count;
                if (products[i].check) {
                    result.price += products[i].count * products[i].price;
                }
                if (product.count > 0 && !product.check) {
                    result.chechAllChange = false;
                }
            }
        }
        result.price = result.price.toFixed(2);
        return result;
    });
    const AllChange = (id) => {
        const status = cartEffect;
        store.commit("allChange", { id, status });
    };
    //圆点点击事件
    const checkChangeClick = (shopId, productId) => {
        store.commit("checkChange", { shopId, productId });
    };
    //清空购物车事件
    const checkDeselectAllClick = (id) => {
        store.commit("clearCart", id);
    };

    return {
        cartEffect,
        id,
        checkChangeClick,
        checkDeselectAllClick,
        AllChange,
    };
};
export const getSettleList = () => {
    const store = useStore();
    const route = useRoute();
    const id = route.params.id;
    const cartList = [];
    let num = 0;
    let count = 0;
    for (const i in store.state.cartList[id]?.productList) {
        const state = store.state.cartList[id]?.productList[i];
        if (state.count > 0 && state.check === true) {
            cartList.push(state);
            num += 1;
            count += state.count;
        }
    }
    const cart = []
    for (const i in store.state.cartList) {
        const copy = JSON.stringify(store.state.cartList[i])
        cart.push(JSON.parse(copy))
    }
    const countList = []//商品数量
    const showList = []//控制商品是否展示
    for (const k in cart) {
        let n = 0;
        let num = 0;
        for (const i in cart[k].productList) {
            cart[k].productList[n] = cart[k].productList[i]
            num += cart[k].productList[n].count;
            n++;
            delete cart[k].productList[i]
        }
        countList.push(num)
        showList.push(n)
    }
    const shopName = store.state.cartList[id]?.shopName || "";
    return { cartList, shopName, num, count, cart, countList, showList };
};